#include <iostream>
#include <unistd.h>
#include <fstream>
#include "person.pb.h"
using namespace std;

string pro_str;
void set_person()
{
    Person obj;
    obj.set_name("mike");
    obj.set_id(1);
    //obj.set_email("mike@qq.com")

    *obj.mutable_email()="mike@qq.com";

    fstream output("pro_fstream", ios::out | ios::trunc | ios::binary);
    if(! output)
    {
        cerr << "set err when write to stream\n";
        exit(0);
    }
    obj.SerializeToOstream(&output);
    output.close();
}

void get_person()
{
    fstream read_stream("pro_fstream",ios::in|ios::binary);
    if(!read_stream)
    {
        cerr<<"get err when read from stream\n";
        exit(0);
    }
    Person p;
    p.ParseFromIstream(&read_stream);
    read_stream.close();        
    cout << "name:"<<p.name()<<endl;
    cout << "id:"<<p.id()<<endl;
    cout << "email:"<<p.email()<<endl;


}

int main()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    set_person();
    get_person();
    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}
